//
//  CNEmoKeyboardCell.swift
//  EmoticonKeyboard
//
//  Created by 陈锦聪 on 16/4/1.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit

private let margin: CGFloat = 3.0
private let bottomMargin: CGFloat = 25.0

protocol CNEmoKeyboardCellDelegate: NSObjectProtocol {
    func didSelectEmoticon(emoticon: CNEmoticonModel)
    func deleteOneEmoticon()
}

class CNEmoKeyboardCell: UICollectionViewCell {
    // MARK: - 属性:
    weak var delegate: CNEmoKeyboardCellDelegate?
    var indexPath: NSIndexPath? {
        didSet {
            recentLabel.hidden = indexPath?.section != 0
        }
    }
    var pageEmos: [CNEmoticonModel]? {
        didSet {
            guard let emoticons = pageEmos else {
                print("cell表情页为空")
                return
            }
            for button in emoButtons {
                button.hidden = true
            }
            for i in 0..<emoticons.count {
                let button = emoButtons[i]
                let emoticon = emoticons[i]
                button.hidden = false
                button.emoticon = emoticon
            }
        }
    }
    // MARK: - 方法:
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        layoutButtons()
    }
    private func prepareUI() {
        addEmoButtons()
        contentView.addSubview(deleteButton)
        contentView.addSubview(recentLabel)
    }
    private func addEmoButtons() {
        for _ in 0..<CNEmoNumberOfPage {
            let button = CNEmoButton()
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            button.addTarget(self, action: "emoticonButtonAction:", forControlEvents: UIControlEvents.TouchUpInside)
            contentView.addSubview(button)
            emoButtons.append(button)
        }
    }
    private func layoutButtons() {
        let itemW = (frame.width - 2 * margin) / CGFloat(CNEmoColumnOfPage)
        let itemH = (frame.height - bottomMargin) / CGFloat(CNEmoRowOfPage)
        for i in 0..<emoButtons.count {
            let button = emoButtons[i]
            let col = i % CNEmoColumnOfPage
            let row = i / CNEmoColumnOfPage
            let x = margin + CGFloat(col) * itemW
            let y = CGFloat(row) * itemH
            button.frame = CGRect(x: x, y: y, width: itemW, height: itemH)
        }
        let delX = CGFloat(CNEmoColumnOfPage - 1) * itemW + margin
        let delY = CGFloat(CNEmoRowOfPage - 1) * itemH
        deleteButton.frame = CGRect(x: delX, y: delY, width: itemW, height: itemH)
        recentLabel.center = CGPoint(x: frame.width * 0.5, y: frame.height - recentLabel.frame.height)
    }
    @objc private func emoticonButtonAction(button: CNEmoButton) {
//        print("\(button.emoticon?.chs)")
        delegate?.didSelectEmoticon(button.emoticon!)
    }
    @objc private func deleteButtonAction() {
        delegate?.deleteOneEmoticon()
    }
    // MARK: - 懒加载:
    /// 表情按钮
    private lazy var emoButtons: [CNEmoButton] = [CNEmoButton]()
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "deleteButtonAction", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    private lazy var recentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.grayColor()
        label.font = UIFont.systemFontOfSize(12)
        label.text = "最近使用的表情"
        label.sizeToFit()
        return label
    }()
}
